package com.controller;

import com.pojo.Content;
import com.pojo.User;
import com.service.ContentService;
import com.service.UserService;
import com.utils.sendEmail;
import com.utils.uploadImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
   private ContentService contentService;

    @RequestMapping(value = "/searchEmail", method = RequestMethod.POST)
    public User searchEmail(@RequestBody User user) {
        User user1 = userService.searchUser(user);
  if (user1 !=null){
      System.out.println("ok");
      System.out.println(user1);

  }else {
      System.out.println("no");
      System.out.println(user1);
  }
  return user1;
    }



/*注册*/
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public String testRegister(@RequestBody User user) {
        System.out.println(user.getEmail() + user.getPwd());
        int i = userService.insertUser(user);
        if (i > 0) {
            return "ok";
        } else {
            return "no";
        }
    }
/*登录*/
    @RequestMapping(value = "/loginUser",method = RequestMethod.POST)
    public Map<String,Object> login(@RequestBody User user){
        boolean flag = userService.loginUser(user);
        User thisUser = userService.searchUser(user);
        Integer userId = thisUser.getId();
        if (flag){
            List<Content> todoList = contentService.findTodoByMonth(userId);
            Map<String,Object> map = new HashMap<>();
            map.put("user",thisUser);
            map.put("todoList",todoList);
            System.out.println("登录成功");
            return map;
        }else {
            return null;
        }
    }

    /*发送图片*/
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String upload( MultipartFile file) throws IOException {
        String upload = uploadImage.upload(file);
        /*添加到数据库*/
       /* user.setAvatar(upload);
        System.out.println(user);
        userService.insertAvatar(user);*/
        System.out.println(upload);
        return upload;
    }

    /*修改头像*/
    @RequestMapping(value = "/change-avatar", method = RequestMethod.POST)
  public int upload(@RequestBody User user){
        int i = userService.insertAvatar(user);
        return i;
    }
    /*发送邮件*/
    @Autowired
    private sendEmail sendEmail;
    @RequestMapping(value = "/email", method = RequestMethod.POST)
    public int email(@RequestBody User user) {
        int code = (int) ((Math.random() * 9 + 1) * Math.pow(10, 5));
        sendEmail.send(user.getEmail(), code);
        return code;
    }
   /* @RequestMapping(value = "/email/{email}", method = RequestMethod.GET)
    public void email(@PathVariable String email) {
      *//*  int code = (int) ((Math.random() * 9 + 1) * Math.pow(10, 5));*//*
        sendEmail.send(email, 456345);
    }*/

    /*设置昵称*/
    @RequestMapping(value = "/nickname", method = RequestMethod.PUT)
    public String updateNickname(@RequestBody User user) {
        String nickname = user.getNickname();
        int i = userService.updateNickname(user);
        if (i > 0) {
            return nickname;
        } else {
            return "no";
        }
    }
    /*修改密码*/
    @RequestMapping(value = "/pwd", method = RequestMethod.PUT)
    public String updatePwd(@RequestBody User user) {
        int i = userService.updatePwd(user);
        if (i > 0) {
            System.out.println(user);
            return "ok";

        } else {
            System.out.println(user);
            return "no";
        }
    }
}
